//
//  RCDLiveTipMessageCell.m
//  RongIMKit
//
//  Created by xugang on 15/1/29.
//  Copyright (c) 2015年 RongCloud. All rights reserved.
//

#import "MyLiveTipMessageCell.h"
#import "RCDLiveTipLabel.h"
#import "RCDLiveKitUtility.h"
#import "RCDLiveKitCommonDefine.h"
#import "RCDLiveGiftMessage.h"
@interface MyLiveTipMessageCell ()<RCDLiveAttributedLabelDelegate>
@end

@implementation MyLiveTipMessageCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.tipMessageLabel = [RCDLiveTipLabel greyTipLabel];
        self.tipMessageLabel.textAlignment = NSTextAlignmentLeft;
        self.tipMessageLabel.userInteractionEnabled = YES;
        self.tipMessageLabel.font = [UIFont systemFontOfSize:16.f];
        
        self.tipMessageLabel.textColor = [UIColor blackColor];
        self.tipMessageLabel.backgroundColor = [UIColor colorWithRed:245/255.0 green:246/255.0 blue:248/255.0 alpha:1];
        self.tipMessageLabel.marginInsets = UIEdgeInsetsMake(0.1f, 0.5f, 4.0f, 0.5f);

        self.tipNickLabel = [RCDLiveTipLabel greyTipLabel];
        self.tipNickLabel.textAlignment = NSTextAlignmentLeft;
        self.tipNickLabel.userInteractionEnabled = YES;
        self.tipNickLabel.font = [UIFont systemFontOfSize:16.f];
        self.tipNickLabel.marginInsets = UIEdgeInsetsMake(0.5f, 0.5f, 0.1f, 0.5f);
        
        self.messageTimeLabel = [RCDLiveTipLabel greyTipLabel];
        self.messageTimeLabel.textAlignment = NSTextAlignmentLeft;
        self.messageTimeLabel.userInteractionEnabled = YES;
        self.messageTimeLabel.font = [UIFont systemFontOfSize:16.f];
        self.messageTimeLabel.marginInsets = UIEdgeInsetsMake(0.5f, 0.5f, 0.1f, 0.5f);
        
        
        UIView *rowView = [[UIView alloc] init ];//:CGRectMake(0, 0, frame.size.width, 40.0f)];
        [rowView addSubview:self.tipNickLabel];
        
        
        self.baseContentView.layoutMargins= UIEdgeInsetsMake(2.0f, 0.5f, 4.0f, 0.5f);
        [self.baseContentView addSubview:rowView];
        [self.baseContentView addSubview:self.tipMessageLabel];

        self.isLandScape = NO;
        
//        [self.baseContentView addSubview:self.tipMessageLabel];
//        self.tipMessageLabel.font = [UIFont systemFontOfSize:16.f];;
//        self.tipMessageLabel.marginInsets = UIEdgeInsetsMake(0.5f, 0.5f, 0.5f, 0.5f);
    }
    return self;
}

- (void)setDataModel:(RCDLiveMessageModel *)model {
    [super setDataModel:model];
    
    if(!self.isLandScape) {
        self.tipMessageLabel.backgroundColor = [UIColor clearColor];
        self.tipMessageLabel.textColor = [UIColor lightGrayColor];
    }
    RCMessageContent *content = model.content;
    if ([content isMemberOfClass:[RCInformationNotificationMessage class]]) {
//        RCInformationNotificationMessage *notification = (RCInformationNotificationMessage *)content;
//        NSString *localizedMessage = [RCDLiveKitUtility formatMessage:notification];
//        self.tipMessageLabel.text = localizedMessage;
//        self.tipMessageLabel.textColor = RCDLive_HEXCOLOR(0xffb83c);

        NSString *localizedMessage = @"加入聊天室";
        
        NSString *str =[NSString stringWithFormat:@"   %@",localizedMessage];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
        
        if(self.isLandScape) {
            [attributedString addAttribute:NSForegroundColorAttributeName value:(RCDLive_HEXCOLOR(0x000000)) range:[str rangeOfString:localizedMessage]];
        }else {
            [attributedString addAttribute:NSForegroundColorAttributeName value:(RCDLive_HEXCOLOR(0xffb83c)) range:[str rangeOfString:str]];
        }
        self.tipMessageLabel.attributedText = attributedString.copy;
            
        
    }else if ([content isMemberOfClass:[RCTextMessage class]]){
        RCTextMessage *notification = (RCTextMessage *)content;
        NSString *localizedMessage = [RCDLiveKitUtility formatMessage:notification];
        NSString *name=@"";
        if (content.senderUserInfo) {
            name = [NSString stringWithFormat:@"%@:",content.senderUserInfo.name];
        }
        NSString *str =[NSString stringWithFormat:@"  %@",localizedMessage];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
        
        if(self.isLandScape) {
            [attributedString addAttribute:NSForegroundColorAttributeName value:([UIColor blackColor]) range:[str rangeOfString:str]];
        }else {
            [attributedString addAttribute:NSForegroundColorAttributeName value:(RCDLive_HEXCOLOR(0xffb83c)) range:[str rangeOfString:str]];
        }
        self.tipMessageLabel.attributedText = attributedString.copy;
    }else if ([content isMemberOfClass:[RCDLiveGiftMessage class]]){
        RCDLiveGiftMessage *notification = (RCDLiveGiftMessage *)content;
        NSString *name=@"";
        if (content.senderUserInfo) {
            name = content.senderUserInfo.name;
        }
        
        NSString *localizedMessage = @"送了一个钻戒";
        if(notification && [notification.type isEqualToString:@"1"]){
          localizedMessage = @"为主播点赞";
        }
        
        NSString *str =[NSString stringWithFormat:@"  %@",localizedMessage];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
        
        //RCDLive_HEXCOLOR(0xf719ff)粉色
        if(self.isLandScape) {
        [attributedString addAttribute:NSForegroundColorAttributeName value:(RCDLive_HEXCOLOR(0x000000)) range:[str rangeOfString:localizedMessage]];
        }else {
            [attributedString addAttribute:NSForegroundColorAttributeName value:(RCDLive_HEXCOLOR(0xffb83c)) range:[str rangeOfString:str]];
        }
//        //建立行间距模型
//        NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
//        //设置行间距
//        [paragraphStyle1 setLineSpacing:5.f];
//        
//        //把行间距模型加入NSMutableAttributedString模型
//        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [str length])];

        
        
        self.tipMessageLabel.attributedText = attributedString.copy;
        
        
    }
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:model.sentTime/1000];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    NSString *str0 =[NSString stringWithFormat:@"%@ %@",content.senderUserInfo.name,confromTimespStr];

    NSMutableAttributedString *attributedString0 = [[NSMutableAttributedString alloc] initWithString:str0];
    if(self.isLandScape) {
    [attributedString0 addAttribute:NSForegroundColorAttributeName value:(RCDLive_HEXCOLOR(0x000000)) range:[str0 rangeOfString:str0]];
    }else {
        [attributedString0 addAttribute:NSForegroundColorAttributeName value:(RCDLive_HEXCOLOR(0xffb83c)) range:[str0 rangeOfString:str0]];
    }
    
    self.tipNickLabel.attributedText=attributedString0.copy;
    
    
    

    NSString *__text = self.tipMessageLabel.text;
    CGSize __labelSize = [MyLiveTipMessageCell getTipMessageCellSize:__text];
    
    NSString *__text0 = self.tipNickLabel.text;
    CGSize __labelSize0 = [MyLiveTipMessageCell getTipMessageCellSize:__text0];

    if (_isFullScreenMode) {
        self.tipNickLabel.frame = CGRectMake(2,0, __labelSize0.width, __labelSize0.height);
        self.tipMessageLabel.frame = CGRectMake(8,__labelSize0.height, __labelSize.width, __labelSize.height);
    }else{
        self.tipNickLabel.frame = CGRectMake((self.baseContentView.bounds.size.width - __labelSize0.width) / 2.0f,0, __labelSize.width, __labelSize.height);
        self.tipMessageLabel.frame = CGRectMake((self.baseContentView.bounds.size.width - __labelSize.width) / 2.0f,__labelSize0.height, __labelSize.width, __labelSize.height);
    }
    CGRect rbase = self.baseContentView.frame;
    
    CGRect tbase = CGRectMake(0, 0, rbase.size.width, self.tipNickLabel.frame.size.height+self.tipMessageLabel.frame.size.height+10);
    self.baseContentView.frame = tbase;
}



- (void)attributedLabel:(RCDLiveAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
    NSString *urlString=[url absoluteString];
    if (![urlString hasPrefix:@"http"]) {
        urlString = [@"http://" stringByAppendingString:urlString];
    }
    if ([self.delegate respondsToSelector:@selector(didTapUrlInMessageCell:model:)]) {
        [self.delegate didTapUrlInMessageCell:urlString model:self.model];
        return;
    }
}

/**
 Tells the delegate that the user did select a link to an address.
 
 @param label The label whose link was selected.
 @param addressComponents The components of the address for the selected link.
 */
- (void)attributedLabel:(RCDLiveAttributedLabel *)label didSelectLinkWithAddress:(NSDictionary *)addressComponents
{
    
}

/**
 Tells the delegate that the user did select a link to a phone number.
 
 @param label The label whose link was selected.
 @param phoneNumber The phone number for the selected link.
 */
- (void)attributedLabel:(RCDLiveAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber
{
    NSString *number = [@"tel://" stringByAppendingString:phoneNumber];
    if ([self.delegate respondsToSelector:@selector(didTapPhoneNumberInMessageCell:model:)]) {
        [self.delegate didTapPhoneNumberInMessageCell:number model:self.model];
        return;
    }
}

-(void)attributedLabel:(RCDLiveAttributedLabel *)label didTapLabel:(NSString *)content
{
    if ([self.delegate respondsToSelector:@selector(didTapMessageCell:)]) {
        [self.delegate didTapMessageCell:self.model];
    }
}

+ (CGSize)getTipMessageCellSize:(NSString *)content{
    CGFloat maxMessageLabelWidth = 220;
    CGSize __textSize = CGSizeZero;
    if (RCDLive_IOS_FSystenVersion < 7.0) {
        __textSize = RCDLive_RC_MULTILINE_TEXTSIZE_LIOS7(content, [UIFont systemFontOfSize:16.0f], CGSizeMake(maxMessageLabelWidth, MAXFLOAT), NSLineBreakByTruncatingTail);
    }else {
        __textSize = RCDLive_RC_MULTILINE_TEXTSIZE_GEIOS7(content, [UIFont systemFontOfSize:16.0f], CGSizeMake(maxMessageLabelWidth, MAXFLOAT));
    }
    __textSize = CGSizeMake(ceilf(__textSize.width)+10 , ceilf(__textSize.height)+6);
    return __textSize;
}
@end
